import * as PIXI from 'pixi.js';
const toastStyle = new PIXI.TextStyle({
  fontSize: 14,
  fill: "white",
  stroke: '#883300',
  strokeThickness: 4,
  dropShadow: true,
  dropShadowColor: "#000000",
  dropShadowBlur: 2,
  dropShadowAngle: Math.PI / 6,
  dropShadowDistance: 2,
});
const infoStyle = new PIXI.TextStyle({
  fontSize: 14,
  fill: "white",
  stroke: '#2196f3',
  strokeThickness: 4,
  dropShadow: true,
  dropShadowColor: "#000000",
  dropShadowBlur: 2,
  dropShadowAngle: Math.PI / 6,
  dropShadowDistance: 2,
});
const errorStyle = new PIXI.TextStyle({
  fontSize: 14,
  fill: "white",
  stroke: '#ee3633',
  strokeThickness: 4,
  dropShadow: true,
  dropShadowColor: "#000000",
  dropShadowBlur: 2,
  dropShadowAngle: Math.PI / 6,
  dropShadowDistance: 2,
});
// 消息显示
export default class MsgService {
  container = new PIXI.Container();
  private msgTop = 0;
  msgHeight = 20;
  private fix = new Map<string, PIXI.Sprite[]>();
  tickerLoop: () => any;
  constructor(private ticker: PIXI.Ticker) {
    // this.container.backgroundColor = 0x0;
    this.container.x = 0;
    this.container.y = 0;
    this.container.width = 300;
    this.container.height = 300;
    this.container.on('childRemoved', () => {
      if (this.tickerLoop) return;
      this.tickerLoop = () => this.loop()
      this.ticker.add(this.tickerLoop)
    })
  }
  // 浮动提示
  toast(msg: string) {
    const m = this.alert(msg, toastStyle);
    setTimeout(() => {
      this.container.removeChild(m)
      this.msgTop = 0;
    }, 5000);
  }

  private isStop() {
    return !this.container.children.length || this.container.children.length * this.msgHeight === this.msgTop
  }
  private loop() {
    for (let i = 0; i < this.container.children.length; i++) {
      const m = this.container.children[i];
      if (m.y - 1 <= 0) {
        continue;
      }
      if (i > 0) {
        const b = this.container.children[i - 1];
        if (b.y + this.msgHeight >= m.y - 1) {
          continue;
        }
      }
      m.y -= 1;
    }
    this.msgTop -= 1;
    if (this.isStop()) {
      this.ticker.remove(this.tickerLoop)
      this.tickerLoop = undefined;
    }
  }
  private alert(msg: string, style: PIXI.TextStyle) {
    const m = new PIXI.Text(msg, style);
    m.x = 2;
    m.y = this.msgTop;
    this.msgTop += this.msgHeight;
    m.buttonMode = true;
    m.interactive = true;
    this.container.addChild(m);
    m.on('pointerdown', () => {
      this.container.removeChild(m)
    });
    return m;
  }
  // 提示
  info(msg: string) {
    this.pushFix(msg, this.alert(msg, infoStyle))
  }
  // 错误
  error(msg: string) {
    this.pushFix(msg, this.alert(msg, errorStyle));
  }
  private pushFix(msg: string, s: PIXI.Sprite) {
    if (!this.fix.has(msg)) {
      this.fix.set(msg, [s])
      return;
    }
    this.fix.get(msg).push(s);
  }
  close(...msgs: string[]) {
    for (const m of msgs) {
      if (this.fix.has(m)) {
        const s = this.fix.get(m)
        this.container.removeChild(...s)
        this.fix.delete(m)
      }
    }
  }
}
